package com.example.employee.util;


import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.example.employee.entity.EmployeeDO;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author
 * @date 2022/4/7
 */
//public class ExcelUtils {
//    public static void downLoadExcel(String fileName, HttpServletResponse response) throws Exception {
//        try {
//            response.setContentType("application/vnd.ms-excel");
//            response.setCharacterEncoding("UTF-8");
//            fileName = URLEncoder.encode(fileName, "UTF-8");
//            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
//
//        } catch (IOException e) {
//            throw new Exception(e.getMessage());
//        }
//    }

public class ExcelUtils {
    public static void downLoadExcel(HttpServletResponse response, List<EmployeeDO> list) throws Exception {
        ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream()).build();

        WriteSheet sheet = EasyExcel.writerSheet(0,"sheet").head(EmployeeDO.class).build();

        excelWriter.write(list ,sheet);
        excelWriter.finish();



    }

/*
    public static Map getExcelHead(String filePath){
        Map[] templateHeadMap = {new HashMap<>()};
        class ExcelListener extends AnalysisEventListener {

            @Override
            public void invokeHead(Map headMap, AnalysisContext context) {

                templateHeadMap[0] = headMap;
            }

            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    public static Map getExcelHead(String filePath){
        Map[] templateHeadMap = {new HashMap<>()};
        class ExcelListener extends AnalysisEventListener {

            @Override
            public void invokeHead(Map headMap, AnalysisContext context) {

                templateHeadMap[0] = headMap;
            }

            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }
        EasyExcel.read(filePath, new ExcelListener()).sheet("sheet1").doRead();
        return templateHeadMap[0];
    }


    public static Map getExcelHead(MultipartFile file) throws IOException {
        Map[] templateHeadMap = {new HashMap<>()};
        class ExcelListener extends AnalysisEventListener {
            @Override
            public void invokeHead(Map headMap, AnalysisContext context) {
                templateHeadMap[0] = headMap;
            }

            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }
        EasyExcel.read(file.getInputStream(),new ExcelListener()).sheet("sheet1").doRead();
        return templateHeadMap[0];
    }



            }
        }
        EasyExcel.read(filePath, new ExcelListener()).sheet("sheet1").doRead();
        return templateHeadMap[0];
    }


    public static Map getExcelHead(MultipartFile file) throws IOException {
        Map[] templateHeadMap = {new HashMap<>()};
        class ExcelListener extends AnalysisEventListener {

            @Override
            public void invokeHead(Map headMap, AnalysisContext context) {
                templateHeadMap[0] = headMap;
            }

            @Override
            public void invoke(Object o, AnalysisContext analysisContext) {

            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        }
        EasyExcel.read(file.getInputStream(),new ExcelListener()).sheet("sheet1").doRead();
        return templateHeadMap[0];
    }
    */


//    /**
//     * 导入excel中数据
//     * initSheet：sheetNo: sheet页码，默认为1；headLineMun: 从第几行开始读取数据，默认为0, 表示从第一行开始读取
//     *
//     * @param inputStream
//     * @return List
//     */
//    public static List<Object> readBySheet(InputStream inputStream) {
//        ExcelListener excelListener = new ExcelListener();
//        EasyExcelFactory.readBySax(inputStream, sheet, excelListener);
//        return excelListener.getDatas();
//    }


}
